package com.mzj.saas.jobs.sms.service;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.mzj.saas.jobs.config.sms.SmsConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 模板短信处理
 * 
 * @author shun
 * @createDate: 2021年3月19日 下午5:40:19
 * @version: 1.0
 *
 */
@Component
public class SmsService {
	private Logger LOG = LoggerFactory.getLogger(SmsService.class);

	@Autowired
	private SmsConfig smsConfig;

	/**
	 * 发送模板短信
	 * @param mobile
	 * @param templateId
	 * @return
	 */
	public String sendTemplateSms(String mobile,String templateId) {
		CCPRestSmsSDK sdk = new CCPRestSmsSDK();
		sdk.init(smsConfig.getServerIp(), smsConfig.getServerPort());
		sdk.setAccount(smsConfig.getAccountSid(), smsConfig.getAccountToken());
		sdk.setAppId(smsConfig.getAppid());
		sdk.setBodyType(BodyType.Type_JSON);

		//String templateId = smsConfig.getNopassTemplateCode();

		HashMap<String, Object> result = sdk.sendTemplateSMS(mobile, templateId, null, null, null);
		if ("000000".equals(result.get("statusCode"))) {
			LOG.info("SmsService sendTemplateNopassSms success, statusCode=" + result.get("statusCode") + " msg= "
					+ result.get("statusMsg"));

			return "success";
		} else {
			// 异常返回输出错误码和错误信息
			LOG.info("SmsService sendTemplateNopassSms fail, errorCode=" + result.get("statusCode") + " msg= "
					+ result.get("statusMsg"));
			return "fail";
		}
	}


	public String sendTemplateSmsWithData(String mobile, String templateId, String[] datas) {
		CCPRestSmsSDK sdk = new CCPRestSmsSDK();
		sdk.init(smsConfig.getServerIp(), smsConfig.getServerPort());
		sdk.setAccount(smsConfig.getAccountSid(), smsConfig.getAccountToken());
		sdk.setAppId(smsConfig.getAppid());
		sdk.setBodyType(BodyType.Type_JSON);

		//String templateId = smsConfig.getNopassTemplateCode();
		HashMap<String, Object> result = sdk.sendTemplateSMS(mobile, templateId, datas, null, null);
		if ("000000".equals(result.get("statusCode"))) {
			LOG.info("SmsService sendTemplateSmsWithData success, statusCode=" + result.get("statusCode") + " msg= "
					+ result.get("statusMsg"));

			return "success";
		} else {
			// 异常返回输出错误码和错误信息
			LOG.info("SmsService sendTemplateSmsWithData fail, errorCode=" + result.get("statusCode") + " msg= "
					+ result.get("statusMsg"));
			return "fail";
		}
	}

}
